﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Threading;

namespace ProDeletor
{
    class Controller
    {   // XML-format and example
        //  <!-- ProDeleter service control -->
        //    <servicecontrol>
        //      <folder path="C:\Data\FtpInput">
        //          <name value="Friendly name of task"/>
        //          <triggertime value="00:15"/>
        //          <weekdays value="1,2,3,4,5,6,7"/>
        //          <recursion value="true"/>
        //          <archive value="true"/>
        //          <archivePath value="C:\Data\Archive"/>
        //          <emptyfolders value="true"/>
        //          <keepdays value="10"/>
        //          <keephours value="0"/>
        //          <keepminutes value="0"/>
        //          <uselog value="false"/>
        //          <logpath value="C:\Logs"/>
        //          <testmode value="false"/>
        //      </folder>
        //    </servicecontrol>

        string path;
        bool recursion;
        bool uselog;
        //string logpath;
        bool archive;
        //string archivePath;
        bool testmode;
        bool emptyfolders;
        int keepminutes;
        string controlFile;
        Task task;
        List<Task> taskList;

        public Controller(string controlFile)
        {
            this.controlFile = controlFile;
        }

        //public void printTask()
        //{
        //    foreach (Task task in taskList)
        //    {
        //        Console.WriteLine("Recursion: " + task.deleter.UseRecursion);
        //        Console.WriteLine("Log: " + task.deleter.WriteLog);
        //        Console.WriteLine("Testmode: " + task.deleter.TestMode);
        //        Console.WriteLine("Time to keep: " + task.deleter.TimeToKeep);
        //        Console.WriteLine("Del empty folders: " + task.deleter.DeleteEmptyFolders);
        //        Console.WriteLine("H: " + task.scheduler.Trigger);
        //        int i=0;
        //        foreach (bool schd in task.scheduler.weekDaysList)
        //        {

        //            Console.WriteLine("Day " + i + ": " + schd);
        //            Console.WriteLine(int.Parse(task.scheduler.Trigger.Split(':')[0].ToString()));
        //            Console.WriteLine(int.Parse(task.scheduler.Trigger.Split(':')[1].ToString()));
        //            i++;
        //        }
        //        Console.WriteLine("\n\n");
        //    }
        //}

        public void readControlFile()
        {
            XmlTextReader textReader = new XmlTextReader(controlFile);
            taskList = new List<Task>();
            while (textReader.Read())
            {
                if (textReader.IsStartElement())
                {
                    switch (textReader.Name.ToLower())
                    {
                        case "folder":
                            string attribute = textReader["path"];
                            if (attribute != null)
                            {
                                path = attribute;
                                task = new Task(path);
                                taskList.Add(task);
                                keepminutes = 0;
                            }
                            break;

                        case "filemask":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.deleter.FileMask = attribute;
                            }
                            break;

                        case "logpath":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.deleter.LogPath = attribute;
                            }
                            break;

                        case "mode":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.deleter.Mode = int.Parse(attribute);
                            }
                            break;

                        case "triggermode":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (int.Parse(attribute) == 0)
                                    task.scheduler.TriggerMode = false;
                                else
                                    task.scheduler.TriggerMode = true;
                            }
                            //task.scheduler.TriggerMode
                            break;

                        case "triggertime":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.scheduler.Trigger = attribute;
                            }
                            break;

                        case "keepdays":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                keepminutes += int.Parse(attribute) * 1440;
                            }
                            break;

                        case "keephours":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                keepminutes += int.Parse(attribute) * 60;
                            }
                            break;

                        case "keepminutes":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                keepminutes += int.Parse(attribute);
                            }
                            task.deleter.TimeToKeep = keepminutes;
                            break;

                        case "recursion":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    recursion = true;
                                else
                                    recursion = false;
                            }
                            task.deleter.UseRecursion = recursion;
                            break;

                        case "emptyfolders":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    emptyfolders = true;
                                else
                                    emptyfolders = false;
                            }
                            task.deleter.DeleteEmptyFolders = emptyfolders;
                            break;

                        case "uselog":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    uselog = true;
                                else
                                    uselog = false;
                            }
                            task.deleter.WriteLog = uselog;
                            break;

                        case "testmode":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    testmode = true;
                                else
                                    testmode = false;
                            }
                            task.deleter.TestMode = testmode;
                            break;

                        case "archive":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    archive = true;
                                else
                                    archive = false;
                            }
                            task.deleter.Archive = archive;
                            break;

                        case "archivefolder":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.deleter.ArchivePath = attribute;
                            }
                            break;

                        // weekdays
                        case "weekdays":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                string[] days = attribute.Split(',');
                                foreach (string day in days)
                                {
                                    if (day.CompareTo("1") == 0)
                                        task.scheduler.Monday = true;
                                    if (day.CompareTo("2") == 0)
                                        task.scheduler.Tuesday = true;
                                    if (day.CompareTo("3") == 0)
                                        task.scheduler.Wednesday = true;
                                    if (day.CompareTo("4") == 0)
                                        task.scheduler.Thursday = true;
                                    if (day.CompareTo("5") == 0)
                                        task.scheduler.Friday = true;
                                    if (day.CompareTo("6") == 0)
                                        task.scheduler.Saturday = true;
                                    if (day.CompareTo("7") == 0)
                                        task.scheduler.Sunday = true;
                                }
                            }
                            break;
                    }
                }
            }
        }

        public void checkTasks()
        {
            DateTime timeNow = DateTime.Now;
            int taskMinutes;
            int nowMinutes;
            foreach (Task task in taskList)
            {
                if (!task.scheduler.TriggerMode)
                {
                    if (int.Parse(timeNow.Minute.ToString()) == int.Parse(task.scheduler.Trigger.Split(':')[1].ToString()) &&
                        (int.Parse(timeNow.Hour.ToString()) == int.Parse(task.scheduler.Trigger.Split(':')[0].ToString()) &&
                        task.scheduler.weekDaysList[(int)timeNow.DayOfWeek]) && !task.Running)
                    {
                        Thread deleterThread = new Thread(new ThreadStart(task.runTask));
                        deleterThread.Start();
                        Thread.Sleep(1000);
                    }
                }
                else
                {
                    taskMinutes = (int.Parse(task.scheduler.Trigger.Split(':')[0].ToString()) * 60) +
                        int.Parse(task.scheduler.Trigger.Split(':')[1].ToString());
                    // Possible division by zero
                    if (taskMinutes < 1)
                        taskMinutes = 1;
                    nowMinutes = (timeNow.Hour * 60) + timeNow.Minute;
                    if (nowMinutes % taskMinutes == 0)
                    {
                        Thread deleterThread = new Thread(new ThreadStart(task.runTask));
                        deleterThread.Start();
                        Thread.Sleep(1000);
                    }
                }
            }
        }
    }
}
